package topevery.syn.data.model.codec;

import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import topevery.syn.data.model.ModelTypeEnum;

import java.io.IOException;
import java.lang.reflect.Type;

public class ModelTypeEnumCodec implements ObjectSerializer, ObjectDeserializer {
    @Override
    public <T> T deserialze(DefaultJSONParser defaultJSONParser, Type type, Object o) {
        Object obj = defaultJSONParser.parse();
        return obj == null ? null : (T) ModelTypeEnum.valueOf((Integer)obj);
    }

    @Override
    public int getFastMatchToken() {
        return 0;
    }

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        ModelTypeEnum modelTypeEnum = (ModelTypeEnum)object;
        int value =modelTypeEnum.getValue();
        serializer.write(value);
    }
}
